package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.PmsCategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.PmsCategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.PmsBrandDao;
import com.atguigu.gulimall.product.entity.PmsBrandEntity;
import com.atguigu.gulimall.product.service.PmsBrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandDao, PmsBrandEntity> implements PmsBrandService {

    @Resource
    private PmsCategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsBrandEntity> page = this.page(
                new Query<PmsBrandEntity>().getPage(params),
                new QueryWrapper<PmsBrandEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateBrand(PmsBrandEntity pmsBrand) {
        if(StringUtils.isNotBlank(pmsBrand.getName())){
            QueryWrapper<PmsCategoryBrandRelationEntity> queryWrapper=new QueryWrapper<>();
            //修改中间表名称
            PmsCategoryBrandRelationEntity categoryBrandRelationEntity=new PmsCategoryBrandRelationEntity();
            queryWrapper.eq("brand_id",pmsBrand.getBrandId());
            categoryBrandRelationEntity.setBrandName(pmsBrand.getName());
            categoryBrandRelationService.update(categoryBrandRelationEntity,queryWrapper);
        }
        this.updateById(pmsBrand);

    }

}